<?php
	if (!isset($included)){
		$included = false;
	}
	
	// Template lesen
	$template->_template_read("./System/Design/Templates/index.html");
	
	// Grundlegende Sprach-Strings-Einsetzen
	$template->_template_assign("CAPTION_FOLDERINFO", (string)$language->legends->value_4);
	$template->_template_assign("CAPTION_INDEX1", (string)$language->common->value_5);
	$template->_template_assign("CAPTION_INDEX2", (string)$language->common->value_2);
	$template->_template_assign("CAPTION_INDEX3", (string)$language->common->value_4);
	$template->_template_assign("CAPTION_INDEX4", (string)$language->common->value_6);
	$template->_template_assign("CAPTION_INDEX5", (string)$language->common->value_7);
	$template->_template_assign("CAPTION_INDEX6", (string)$language->common->value_8);
	$template->_template_assign("CAPTION_INDEX7", (string)$language->common->value_9);
	$template->_template_assign("CAPTION_INDEX8", (string)$language->common->value_10);
	$template->_template_assign("CAPTION_INDEX9", (string)$language->common->value_11);
	$template->_template_assign("CAPTION_INDEX10", (string)$language->common->value_12);
	$template->_template_assign("CAPTION_INDEX11", (string)$language->common->value_13);
	$template->_template_assign("CAPTION_INDEX12", (string)$language->common->value_14);
	$template->_template_assign("CAPTION_INDEX13", (string)$language->common->value_15);
	$template->_template_assign("CAPTION_INDEX14", (string)$language->msgs->value_2);
	$template->_template_assign("CAPTION_INDEX15", (string)$language->buttons->value_5);
	$template->_template_assign("CAPTION_INDEX16", (string)$language->common->value_39);
	$template->_template_assign("CAPTION_INDEX17", (string)$language->common->value_44);
	$template->_template_assign("CAPTION_INDEX18", (string)$language->common->value_45);
	$template->_template_assign("CAPTION_INDEX19", (string)$language->common->value_46);
	
	// Ordnerbookmark setzen
	if (isset($_GET["dir"]) AND $_GET["dir"] != "/" AND $_GET["dir"] != "" AND file_exists($_GET["dir"]) AND $included != true){
		$ordner = $_GET["dir"];
	} elseif ($included == true AND file_exists($ordner)) {
		$ordner = $ordner;
	} else {
		$ordner = "./";
	}
	
	$ordner = path_compl($ordner);
	
	// Nachlesen, ob Ordner ein System-Ordner ist
	if (is_restricted($ordner)){
		$ordner = "./";
		oyster_log("ERROR", (string)$language->errors->value_3, "Oyster index.include");
		$head->_template_assign("ERROR", (string)$language->errors->value_3);
	}
	
	// Header momentanes Verzeichnis mitteilen
	$head->_template_assign("DIR", $ordner);
	
	// Überprüfen, ob der Ordner existiert
	if (isset($_GET["dir"]) && !file_exists($_GET["dir"])){
		oyster_log("ERROR", (string)$language->errors->value_4, "Oyster index.include");
		$head->_template_assign("ERROR", (string)$language->errors->value_4);
	} elseif ($included == true AND !file_exists($ordner)) {
		oyster_log("ERROR", (string)$language->errors->value_4, "Oyster index.include");
		$head->_template_assign("ERROR", (string)$language->errors->value_4);
	}
	
	// Ornder in Template schreiben
	$template->_template_assign("FOLDER", $ordner);
	
	// Übergeordnetes directore auslesen und in Template schreiben
	$dirname = path_compl(dirname($ordner));
	if ($ordner != "./"){
		$template->_template_assign("UPPER", $dirname);
	}
	
	// Titel in Headertemplate schreiben
	$head->_template_assign("TITEL", sprintf((string)$language->titles->value_2, $ordner));
	
	/*
		Ornder auslesen
	*/
	
	$folder = 0;
	$files = 0;
	
	// Previewable auslesen
	include("./System/Security/previewable.php");
	
	// BG Color-Variable für verschiedene Hintergrundfarben der Tabelle bestimmen
	$bg_color = 1;
	
	// Verzeichnis öffnen
	$handle = opendir($ordner);
	while ($file = readdir ($handle)) {
		$entrys = array();
		// Array für _template_append() erstellen
		if(is_dir($ordner."/".$file)) {
			if ($file != "." && $file != "..") {
				if ($bg_color == 1){
					$entrys["BG_COLOR"] = "#dedede";
					$bg_color++;
				} else {
					$entrys["BG_COLOR"] = "#efefef";
					$bg_color = 1;
				}
				// Kompletten Pfad speichern
				$compl = $ordner.$file;
				// Änderungsdatum auslesen und sichern
				$filemtime = date("d.m.y G:i", filemtime($compl));
				// Dateigröße auslesen und sichern
				$filesize = oyster_filesize(filesize($compl));
				// File permissions auslesen
				$fileperms = substr(decoct(fileperms($compl)),1);
				// Template füllen
				$entrys["BEZEICHNUNG"] = $file;
				$entrys["ÄNDERUNGSDATUM"] = $filemtime;
				$entrys["GRÖSSE"] = $filesize;
				$entrys["ART"] = (string)$language->common->value_2;
				if (array_key_exists($compl, $system_folders)){
					$entrys["TYPE"] = $system_folders[$compl];
				} else {
					$entrys["TYPE"] = "dir";
				}
				$entrys["GET"] = "dir";
				$entrys["COMPL"] = path_compl($compl);
				$entrys["CHMOD"] = $fileperms;
				// Entrys an Template anhängen
				$template->_template_append("ENTRYS", $entrys);
				$folder ++;
			}
		} else {
			if (!isset($_GET["show_all"])){
				$show_all = 1;
			} else {
				$show_all = $_GET["show_all"];
			}
			if (substr($file, 0, $show_all) != ".") {
				if ($bg_color == 1){
					$entrys["BG_COLOR"] = "#dedede";
					$bg_color++;
				} else {
					$entrys["BG_COLOR"] = "#efefef";
					$bg_color = 1;
				}
				// Kompletten Pfad speichern
				$compl = $ordner.$file;
				// Änderungsdatum auslesen und sichern
				$filemtime = date("d.m.y G:i", filemtime($compl));
				// Dateiinfos auslesen
				$path_parts = pathinfo($compl);
				// Erweiterung auslesen und in Großbuchstaben sichern
				$extension = strtoupper($path_parts["extension"]);
				// Dateigröße auslesen und sichern
				$filesize = oyster_filesize(filesize($compl));
				// File permissions auslesen
				$fileperms = substr(decoct(fileperms($compl)),2);
				// Template füllen
				$entrys["BEZEICHNUNG"] = $file;
				$entrys["COMPL"] = $compl;
				$entrys["ÄNDERUNGSDATUM"] = $filemtime;
				$entrys["GRÖSSE"] = $filesize;
				$extension = strtoupper($extension);
				if (array_key_exists($extension, $filetypesupport)){
					$type = $filetypesupport[strtoupper($extension)];
				} else {
					$type = sprintf((string)$language->common->value_3,$extension);
				}
				if (!is_restricted($file)){
					$entrys["NOT_RES"] = true;
				}
				$entrys["ART"] = $type;
				$entrys["DNLD"] = "file";
				$entrys["CHMOD"] = $fileperms;
				// Bild für Dateierweiterung erkennen
				if (file_exists("System/Design/Icons/Filetypesupport/".$path_parts["extension"].".png")) {
					$entrys["TYPE"] = $path_parts["extension"];
				} else {
					$entrys["TYPE"] = "file";
				}
				if (key_exists(strtolower($extension), $previewable)){
					if ($previewable[strtolower($extension)] != "Browser"){
						$entrys["PREV"] = $previewable[strtolower($extension)]."&amp;file=".$compl;
					} else {
						$entrys["PREV"] = $compl;
					}
				}
				// Entrys an Template anhängen
				$template->_template_append("ENTRYS", $entrys);
				$files ++;
			}
		}
	}
	// Verzeichnis schließen
	closedir($handle);
	
	// Zeige MOVE-TO Ordnern an, falls gewünscht
	$folders_boot = "./System/Kernel/folders.boot";
	if (ALLOW_EASY_MOVE == true){
		if (file_exists($folders_boot)){
			$db_boot = sqlite_open($folders_boot);
			$array_boot = sqlite_array_query($db_boot, "SELECT folder FROM folders", SQLITE_ASSOC);
			$entrys = array();
			foreach ($array_boot as $entry){
				$entrys["MOVE_FULL"] = $entry["folder"];
				$template->_template_append("MOVE_TO", $entrys);
			}
		}
	}
	
	// Erweiterte Ordner-Informationen
	$template->_template_assign("CURRENT_CHMOD", substr(decoct(fileperms($ordner)),1));
	$template->_template_assign("CURRENT_FOLDER", $folder);
	$template->_template_assign("CURRENT_FILES", $files);
?>